﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SMTC.BO.UserPower;
using SMTC.Model.Model;
using SMTC.WebCommon.Common;

public partial class UserPower_UserLogin : BasePage
{
    private UserPowerServices _services = new UserPowerServices();
    private Language la = new Language();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            HttpCookie cookie = Request.Cookies["userinfo"];

            if (null == cookie)
            {
                txtUserName.Focus();
            }
            else
            {
                string name = Request.Cookies["userinfo"]["name"];
                string pwd = Request.Cookies["userinfo"]["pwd"];
                txtUserName.Text = name;
                txtPassword.Attributes["value"] = pwd;
                CheckBoxRemberme.Checked = true;
            }           
        }

        if (Request.Params["selectlanguage"] != null && Request.Params["selectlanguage"] != "")
        {
            Session["selectlanguage"] = Request.Params["selectlanguage"];
            la.LanChange(Session["selectlanguage"].ToString());

            CheckBoxRemberme.Text = Resources.Resource.LoginRememberMe.ToString();
            btn_Login.Text = Resources.Resource.Login.ToString();
            Buttonreset.Text = Resources.Resource.Reset.ToString();
        }
    }

    //protected void lbtn_ZhLanguage_Click(object sender, EventArgs e)
    //{
    //    la.LanChange("1");
    //}

    //protected void lbtn_EnLanguage_Click(object sender, EventArgs e)
    //{
    //    la.LanChange("2");
    //}
    protected void btn_Login_Click(object sender, EventArgs e)
    {
        HttpCookie cookie = new HttpCookie("userinfo");
        SMT_UserInfo Sessionuser = null;
        if (CheckBoxRemberme.Checked == true)
        {
            if (_services.ValidationUserPower(txtUserName.Text, txtPassword.Text, ref Sessionuser) == true)
            {
                cookie.Values["name"] = txtUserName.Text.Trim().ToString();
                cookie.Values["pwd"] = txtPassword.Text.Trim().ToString();
                cookie.Expires = DateTime.Now.AddDays(7);
                Response.Cookies.Add(cookie);
                Session["STMCUser"] = Sessionuser;
                GotoDefault();
            }
            else
            {
                LoginError();

            }
        }
        else
        {

            if (_services.ValidationUserPower(txtUserName.Text, txtPassword.Text, ref Sessionuser) == true)
            {
                cookie.Expires = DateTime.Now.AddDays(-1);
                Response.Cookies.Add(cookie);
                Session["STMCUser"] = Sessionuser;
                GotoDefault();
            }
            else
            {
                LoginError();
            }

        }

    }
    protected void LoginError()
    {
        ClientScript.RegisterStartupScript(Page.GetType(), "", "LoginFalse()", true);

    }
    protected void GotoDefault()
    {
        Session["UserName"] = txtUserName.Text.Trim().ToString();
        Response.Write("<script>window.location='../Default.aspx'</script>");
        //this.Server.Transfer("~/Default.aspx");


    }
    protected void Buttonreset_Click(object sender, EventArgs e)
    {
        txtUserName.Text = "";
        txtPassword.Attributes.Clear();
        CheckBoxRemberme.Checked = false;

    }

}